﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 自定义对象池
/// 对于每一个prefab都有一个list作为对象池
/// 用gameobj的active来激活与启用
/// </summary>
[System.Serializable]
public class ObjPool:Model {
    [SerializeField]
    private OnePool[] pools;
    public override void Init()
    {
        //Debug.Log("init");
        for (int i = 0; i < pools.Length; i++)
        {
            int count = pools[i].initCount;
            pools[i].pool = new List<GameObject>(count);
            for (int j = 0; j < count; j++)
            {
                GameObject go = GetOneObj(i);
                SetUsing(go,false);
                pools[i].pool.Add(go);
            }
        }
    }

    /// <summary>
    /// 从对象池中获取一个可用对象
    /// </summary>
    /// <param name="index"></param>
    /// <param name="pos"></param>
    /// <param name="qua"></param>
    /// <returns></returns>
    public GameObject Initiate(int index,Vector3 pos,Quaternion qua)
    {
        List<GameObject> tempPool = pools[index].pool;
            for (int i = 0; i < tempPool.Count; i++)
            {
                GameObject got = tempPool[i];
                if (got==null)
                {
                    tempPool.RemoveAt(i);
                    continue;
                }
                if (!got.activeInHierarchy)
                {
                    SetUsing(got, true);
                got.transform.position = pos;
                got.transform.rotation = qua;
                    return got;
                    
                }
            }
            GameObject go = GetOneObj(index);
            tempPool.Add(go);
            SetUsing(go, true);
            return go;

        
   
    }
    public GameObject Initiate(int index, Vector3 pos){ return Initiate(index, pos, Quaternion.identity); }
    public T Initiate<T>(int index,Vector3 pos) where T:Component
    {
        GameObject go = Initiate(index, pos);
        return go.GetComponent<T>();
    }
    public T[] Initiates<T>(int index, Vector3 pos) where T : Component
    {
        GameObject go = Initiate(index, pos);
        return go.GetComponents<T>();
    }
    /// <summary>
    /// 得到一个索引下模板的复制体
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    private GameObject GetOneObj(int index)
    {
        GameObject go = GameObject.Instantiate(pools[index].prefab);
        go.transform.parent = Facade.Instance.transform;
        return go;
    }

    /// <summary>
    /// 设置物体的启用/关闭
    /// </summary>
    /// <param name="go"></param>
    /// <param name="value"></param>
    protected virtual void SetUsing(GameObject go,bool value)
    {
        go.SetActive(value);
    }

    public override void Unload()
    {
        //fix me
    }
    public void Unload(int index)
    {
        //fix me
    }
}
[System.Serializable]
public struct OnePool
{
    public GameObject prefab;
   // [HideInInspector]
    public List<GameObject> pool;
    [Tooltip("初始化需要的个数")]
    public int initCount;

}

